<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>用户管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">角色</label>
          <div class="layui-input-block">
            <select name="role" lay-verify="required" id="role">
              <option value="">选择角色</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn" lay-submit lay-filter="log-search" id="search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
        <div class="layui-inline">
          <button class="layui-btn" lay-filter="log-search" id="reset">
            重置
          </button>
        </div>
      </div>

    </div>

    <div class="layui-card-body">
      <table id="demo" class="layui-hide" lay-filter="test"></table>
    </div>
  </div>
</div>
<script type="text/html" id="imgTpl">
  <input type="hidden" id="img" value={{ d.img }} >
  <img style="display: inline-block; width: 100%; height: 100%;" src= {{ d.img }}>
</script>
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="more"><i class="layui-icon layui-icon-list"></i>详情</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn" lay-event="add">新增</button>
    <button class="layui-btn layui-btn-danger " lay-event="delAll">批量删除</button>
  </div>
</script>
<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use([ 'laydate','layer', 'table'], function () {
        var $ = layui.$,
            form = layui.form,
            laydate = layui.laydate,
            table = layui.table,
            layer = layui.layer;

        $.ajax({
            url:"/role/selectRoleList",
            type:"GET",
            dataType:"json",
            success:function(result){
                var list = result;    //返回的数据
                var role = document.getElementById("role");        //add_role_name给select定义的id
                for(var i=0;i<list.length;i++){
                    var option = document.createElement("option");    // 创建添加option属性
                    option.setAttribute("value",list[i].id);                  // 给option的value添加值
                    option.innerText=list[i].name;             // 打印option对应的纯文本 （超级管理员、管理员）
                    role.appendChild(option);                          // 给select 添加option子标签
                }
                form.render("select");                                // 刷性select，显示出数据
            }
        });

        //表格初始化
        table.render({
            elem : '#demo',
            id : 'idTest',
            url : '/getAdmins',
            page : true,
            toolbar: '#toolbarDemo',
            height : 'full-130',
            cols : [ [ //表头
                {type: 'checkbox', fixed: 'left'},
                {
                    field : 'id',
                    title : 'ID',
                    width : 60,
                    sort : true,
                    templet:'<div>{{ d.id }}</div>'
                }, {
                    field : 'name',
                    title : '用户名',
                    width : 100,
                    templet:'<div>{{ d.name }}</div>'
                }, {
                    field : 'img',
                    title : '头像',
                    width : 60,
                    templet: "#imgTpl",
                    event: 'show'
                }, {
                    field : 'phone',
                    title : '手机',
                    width : 130,
                    templet:'<div>{{ d.phone }}</div>'
                }, {
                    field : 'email',
                    title : '邮箱',
                    width : 220,
                    templet:'<div>{{ d.email }}</div>'
                }, {
                    field : 'sex',
                    title : '性别',
                    width : 60,
                    templet:'<div>{{ d.sex }}</div>'
                }, {
                    field : 'name',
                    title : '角色',
                    sort : true,
                    width : 100,
                    templet:'<div>{{ d.role.name }}</div>'
                }, {
                    fixed: 'right', title:'操作', toolbar: '#barDemo', width:220
                } ] ]
        });

        //监听搜索
        $('#search').on('click', function() {
            var role = $('#role');
            if (role.val() != '') {
                table.reload('idTest', {
                    url : '/getAdminsByRoleId',
                    page : {
                        curr : 1
                    },
                    where : {
                        role_id : role.val()
                    }
                });
            } else {
                layer.msg('请选择角色', {
                    icon: 2,
                    time: 1500
                });
            }
        });
        //数据重置
        $('#reset').on('click', function() {
            $('#role').val('');

            table.reload('idTest', {
                url : '/getAdmins',
                page : {
                    curr : 1
                }
            });
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                ,checkData = checkStatus.data,
                formData = [];
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2
                        ,title: '添加角色'
                        ,content: '/addAdminUI'
                        ,maxmin: true
                        ,area: ['700px', '450px']
                        ,btn: ['保存', '取消']
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'addAdmin'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                var field = data.field; //获取提交的字段
                                $.ajax({
                                    type:"post",
                                    contentType : 'application/json',
                                    url:"/addAdmin",
                                    data:JSON.stringify(field),
                                    dataType : 'json',
                                    success:function(data) {
                                        if (data.code == 200){
                                            layer.msg('添加成功',
                                                {
                                                    icon:1,
                                                    time: 1000
                                                },function(){
                                                    table.reload('idTest', {
                                                        url : '/getAdmins',
                                                        page : {
                                                            curr : 1
                                                        }
                                                    });
                                                    layer.close(index); //关闭弹层
                                                });
                                        }else {
                                            layer.msg("后台出错");
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    break;
                case 'delAll':
                    if (checkData.length == 0){
                        layer.msg("不能为空");
                    } else {
                        layer.confirm('是否删除此记录?', {icon: 3, title:'提示'}, function(index){
                            for (var i = 0;i<checkData.length;i++){
                                formData.push(checkData[i].id);
                            }
                            $.ajax({
                                type:"delete",
                                contentType : 'application/json',
                                url:"/delAdmin/"+formData,
                                data:{},
                                dataType : 'json',
                                success:function(data) {
                                    if (data.code == 200){
                                        layer.msg('删除成功',
                                            {
                                                icon:1,
                                                time: 1000
                                            },function(){
                                                table.reload('idTest', {
                                                    url : '/getAdmins',
                                                    page : {
                                                        curr : 1
                                                    }
                                                });
                                                layer.close(index); //关闭弹层
                                            });
                                    }else {
                                        layer.msg("后台出错");
                                    }
                                }
                            });
                        });
                    }
                    break;
            };
        });
        //监听工具条
        //监听事件
        table.on('tool(test)', function(obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    layer.open({
                        type: 2
                        ,title: '编辑管理员'
                        ,content: '/editAdminUI/'+data.id
                        ,maxmin: true
                        ,area: ['700px', '450px']
                        ,btn: ['保存', '取消']
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'addAdmin'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                var field = data.field; //获取提交的字段
                                $.ajax({
                                    type:"post",
                                    contentType : 'application/json',
                                    url:"/admin/updateAdmin",
                                    data:JSON.stringify(field),
                                    dataType : 'json',
                                    success:function(data) {
                                        if (data.code == 200){
                                            layer.msg('更新成功',
                                                {
                                                    icon:1,
                                                    time: 1000
                                                },function(){
                                                    table.reload('idTest', {
                                                        url : '/getAdmins',
                                                        page : {
                                                            curr : 1
                                                        }
                                                    });
                                                    layer.close(index); //关闭弹层
                                                });
                                        }else {
                                            layer.msg("后台出错");
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    break;
                case 'del':
                    layer.confirm('是否删除此记录?', {icon: 3, title:'提示'}, function(index){
                        $.ajax({
                            type:"delete",
                            contentType : 'application/json',
                            url:"/delAdmin/"+data.id,
                            data:{},
                            dataType : 'json',
                            success:function(data) {
                                if (data.code == 200){
                                    layer.msg('删除成功',
                                        {
                                            icon:1,
                                            time: 1000
                                        },function(){
                                            table.reload('idTest', {
                                                url : '/getAdmins',
                                                page : {
                                                    curr : 1
                                                }
                                            });
                                            layer.close(index); //关闭弹层
                                        });
                                }else {
                                    layer.msg("后台出错");
                                }
                            }
                        });
                    });
                    break;
                case 'more':
                    layer.open({
                        type: 2
                        ,title: '查看详情'
                        ,content: '/editAdminUI/'+data.id
                        ,maxmin: true
                        ,area: ['700px', '450px']
                        ,btn: ['确定']
                        ,yes: function(index, layero){
                            layer.close(index); //关闭弹层
                        }
                    });
                    break;
                case 'show':
                    layer.photos(
                        {photos: {title: "查看头像", data: [{src: data.img}]}, shade: .01, closeBtn: 1, anim: 5}
                    );
                    break;
            };
        });
    });
</script>
</body>
</html>
